package com.mes_for_xxxxxx.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mes_for_xxxxxx.entity.Equipment;
import com.mes_for_xxxxxx.entity.common.PageInfo;
import com.mes_for_xxxxxx.entity.common.Result;
import com.mes_for_xxxxxx.enums.EquipStatus;
import com.mes_for_xxxxxx.service.IEquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * <p>
 * 设备信息表 前端控制器
 * </p>
 *
 * @author 179005
 * @since 2024-09-10
 */
@RestController
@RequestMapping("/equipment")
public class EquipmentController {
    @Autowired
    private IEquipmentService equipmentService;

    @PostMapping("/add")
    public Result addEquipment(@RequestBody Equipment equipment){
        //name type location
        equipment.setStatus(EquipStatus.UN_USED.getCode());
        equipment.setCreatedAt(LocalDateTime.now());
        equipment.setLastMaintenance(LocalDate.now());
        equipmentService.save(equipment);
        return Result.success();
    }

    @PostMapping("/page")
    public Result pageEquipment(@RequestBody PageInfo pageInfo){
        int pageNum = pageInfo.getPageNum();
        int pageSize = pageInfo.getPageSize();
        Page<Equipment> page = new Page<>(pageNum, pageSize);
        return Result.success(equipmentService.page(page));
    }

    @GetMapping("/status/{id}/{status}")
    public Result updateStatus(@PathVariable Integer id, @PathVariable Integer status){
        equipmentService.updateStatus(id, status);
        return Result.success();
    }

}
